/*
 * @Author: xiaosihan 
 * @Date: 2024-07-02 11:57:47 
 * @Last Modified by: xiaosihan
 * @Last Modified time: 2024-07-02 14:28:16
 */
import * as BABYLON from "@babylonjs/core";
import { CreateLineSystemVertexData } from "@babylonjs/core/Meshes/Builders/linesBuilder";
import { ArrayItem } from "@babylonjs/loaders/glTF/2.0";
// import { CreateLineSystemVertexData } from "@babylonjs/core/Meshes/linesMesh";

// 三维轴线
export default class Axis extends BABYLON.Node {

    constructor(length: number = 10, scene: BABYLON.Scene) {
        super("axis", scene, true);

        this.x_axis = new BABYLON.LinesMesh("x_axis", scene, this);
        this.y_axis = new BABYLON.LinesMesh("y_axis", scene, this);
        this.z_axis = new BABYLON.LinesMesh("z_axis", scene, this);

        this.init(length);
    }

    x_axis!: BABYLON.LinesMesh;
    y_axis!: BABYLON.LinesMesh;
    z_axis!: BABYLON.LinesMesh;

    init(length: number = 10) {

        // 创建x 轴线
        const x_vertexData = CreateLineSystemVertexData({
            lines: [
                [
                    new BABYLON.Vector3(0, 0, 0), new BABYLON.Vector3(length, 0, 0),

                ],
                // 创建x 刻度线
                ...new Array(length * 10).fill(0).map((v, i) => {
                    const width = (i % 10 === 0) ? 0.2 : 0.05;
                    return [new BABYLON.Vector3(i / 10, 0, -width), new BABYLON.Vector3(i / 10, 0, width)]
                })
            ],
            colors: [
                [
                    new BABYLON.Color4(1, 0, 0, 1), new BABYLON.Color4(0.5, 0, 0, 1),
                ],
                ...new Array(length * 10).fill(0).map(v => {
                    return [new BABYLON.Color4(1, 0, 0, 1), new BABYLON.Color4(1, 0, 0, 1)]
                })
            ]
        });
        // 应用到x轴模型对象上
        x_vertexData.applyToMesh(this.x_axis);

        // 创建y 轴线
        const y_vertexData = CreateLineSystemVertexData({
            lines: [
                [
                    new BABYLON.Vector3(0, 0, 0), new BABYLON.Vector3(0, length, 0),
                ],
                // 创建y 刻度线
                ...new Array(length * 10).fill(0).map((v, i) => {
                    const width = (i % 10 === 0) ? 0.2 : 0.05;
                    return [new BABYLON.Vector3(width, i / 10, -width), new BABYLON.Vector3(-width, i / 10, width)]
                })
            ],
            colors: [
                [new BABYLON.Color4(0, 1, 0, 1), new BABYLON.Color4(0, 0.5, 0, 1)],
                ...new Array(length * 10).fill(0).map(v => {
                    return [new BABYLON.Color4(0, 1, 0, 1), new BABYLON.Color4(0, 1, 0, 1)]
                })
            ]
        });
        // 应用到y轴模型对象上
        y_vertexData.applyToMesh(this.y_axis);

        // 创建z 轴线
        const z_vertexData = CreateLineSystemVertexData({
            lines: [
                [new BABYLON.Vector3(0, 0, 0), new BABYLON.Vector3(0, 0, length)],
                // 创建z刻度线
                ...new Array(length * 10).fill(0).map((v, i) => {
                    const width = (i % 10 === 0) ? 0.2 : 0.05;
                    return [new BABYLON.Vector3(-width, 0, i / 10), new BABYLON.Vector3(width, 0, i / 10)]
                })
            ],
            colors: [
                [new BABYLON.Color4(0, 0, 1, 1), new BABYLON.Color4(0, 0, 0.5, 1)],
                ...new Array(length * 10).fill(0).map(v => {
                    return [new BABYLON.Color4(0, 0, 1, 1), new BABYLON.Color4(0, 0, 1, 1)]
                })
            ]
        });
        // 应用到z轴模型对象上
        z_vertexData.applyToMesh(this.z_axis);

    }



}
